package controllers

import (
	"github.com/gin-gonic/gin"
	"strconv"
	"testGin/model/mysql"
	"testGin/model/tool"
)

type AdminController struct{
	admins mysql.Admins
	roles mysql.Role
	adminRole mysql.AdminRole
}

func (ctr AdminController) RoleAjax(c *gin.Context)  {

	adminId := c.PostForm("adminId")
	if adminId == "" {
		tool.Error(c,0,"参数错误-adminId")
		return
	}

	rid := c.PostForm("rid")
	if rid == "" {
		tool.Error(c,0,"参数错误-rid")
		return
	}

	adminIdInt,_ := strconv.Atoi(adminId)
	ridInt,_ := strconv.Atoi(rid)


	ctr.adminRole.IdAdmin = uint(adminIdInt)
	ctr.adminRole.IdRole = uint(ridInt)

    derr := mysql.AdminRole{}.DelByAdminId(ctr.adminRole.IdAdmin)
	if derr != nil {
		tool.Error(c,0,derr.Error())
		return
	}
    serr := ctr.adminRole.Save(ctr.adminRole);
    if serr != nil {
		tool.Error(c,0,serr.Error())
    	return
	}

	tool.SuccessAdmin(c,200,"操作成功",0,"");
}

func (ctr AdminController) Role(c *gin.Context)  {

	adminId := c.DefaultQuery("id","0")

	adminIdInt,err := strconv.Atoi(adminId)
	if err!= nil  {
		tool.Error(c, 0, err.Error())
		return
	}

	admin,aerr := ctr.admins.GetItemById(adminIdInt)
    if aerr != nil {
		tool.Error(c, 0, aerr.Error())
		return
	}

	roleList,roleErr := ctr.roles.GetLists()

	if roleErr!= nil  {
		tool.Error(c, 0, err.Error())
		return
	}

	roleId := ctr.adminRole.GetRidByAdmin(admin.Id);


	c.HTML(200, "admin/admin/role.html",gin.H{
		"item":admin,
		"list":roleList,
		"roleId":roleId,
	})
}

func (ctr AdminController) Index(c *gin.Context) {
	c.HTML(200, "admin/admin/index.html", gin.H{})
}
func (ctr AdminController) GetList(c *gin.Context) {


	page := c.DefaultQuery("page", "1")
	limit := c.DefaultQuery("limit", "15")
	where := []map[string]interface{}{}
	pageInt, err := strconv.Atoi(page)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	limitInt, err := strconv.Atoi(limit)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	adminId := c.Query("adminId")
	if adminId != "" {
		where = tool.MapWhere("id_admin","=",adminId)
	}
	account := c.DefaultQuery("account", "")
	if account != "" {
		where = tool.MapWhere("account","=",account)
	}
	name := c.DefaultQuery("name", "")
	if name != "" {
		where = tool.MapWhere("name","=",name)
	}
	result, err := ctr.admins.GetList(where, pageInt, limitInt)
	if err != nil {
		tool.Error(c, 1, err.Error())
		return
	}
	tool.SuccessAdmin(c, 0, "请求成功", result["count"].(int), result["data"])
}

func (ctr AdminController) Add(c *gin.Context) {

	c.HTML(200, "admin/admin/add.html","")
}

func (ctr AdminController) AddAjax(c *gin.Context) {


	if err := c.Bind(&ctr.admins); err != nil {
		tool.Error(c, 0, err.Error())
		return
	}

	acode := tool.GetAcode()
	ctr.admins.Acode = acode
	ctr.admins.Password = tool.GetPass(ctr.admins.Password,acode);
	ctr.admins.Status = 1

	errs := mysql.Admins{}.Add(ctr.admins)
	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}
	tool.Success(c, 200, "添加成功", "")
}

func (ctr AdminController) Edit(c *gin.Context) {

	id := c.DefaultQuery("id", "0")


	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id不正确")
		return
	} else {
		node, _ := ctr.admins.GetItemById(ids)
		c.HTML(200, "admin/admin/edit.html", gin.H{
			"item": node,
		})
	}
}

func (ctr AdminController) EditAjax(c *gin.Context) {

	if err := c.Bind(&ctr.admins); err != nil {
		tool.Error(c, 0, err.Error())
		return
	}

	if ctr.admins.Password != "" {
		ctr.admins.Password = tool.GetPass(ctr.admins.Password,ctr.admins.Acode);
	}
	errs := mysql.Admins{}.Save(ctr.admins)

	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}
	tool.Success(c, 200, "修改成功", "")
}
func (ctr AdminController) Del(c *gin.Context) {

	id := c.DefaultQuery("id", "0")
	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id参数错误")
		return
	} else {

		errs := ctr.admins.Del(ids)
		if errs != nil {
			tool.Error(c, 0, errs.Error())
			return
		}
		tool.Success(c, 200, "删除成功", "")
	}

}
func (ctr AdminController) Status(c *gin.Context) {
	id := c.DefaultQuery("id", "0")
	status := c.DefaultQuery("status", "0")

	ids, err := strconv.Atoi(id)
	if  ids < 0 {
    	tool.Error(c,0,err.Error())
    	return
	}

	statuss, err := strconv.Atoi(status);
	if  statuss < 0 {
		tool.Error(c,0,err.Error())
		return
	}

	errs := ctr.admins.SetStatus(ids,statuss)

	if errs != nil{
		tool.Error(c,0,errs.Error())
		return
	}

	tool.Success(c,200,"操作成功","")

}
func (ctr AdminController) Px(c *gin.Context) {

	id := c.DefaultQuery("id", "0")
	px := c.DefaultQuery("px", "0")

	ids, err := strconv.Atoi(id)
	if  ids < 0 {
		tool.Error(c,0,err.Error())
		return
	}

	pxs, err := strconv.Atoi(px);
	if  pxs < 0 {
		tool.Error(c,0,err.Error())
		return
	}

	errs := ctr.admins.Sort(ids,pxs)

	if errs != nil{
		tool.Error(c,0,errs.Error())
		return
	}

	tool.Success(c,200,"操作成功","")
}
